package oracle.apps.financials.collection.fragment;

import oracle.apps.financials.collection.R;
import oracle.apps.financials.collection.adapter.PercentCurrentAdapter;
import oracle.apps.financials.collection.listener.BusinsessUnitWindowListener;
import oracle.apps.financials.collection.listener.PercentCurrentParameterWindowListener;
import oracle.apps.financials.collection.po.Customer;
import oracle.apps.financials.collection.widget.PageIndicator;
import oracle.apps.financials.collection.widget.PagedView;
import oracle.apps.financials.collection.widget.PagedView.OnPagedViewChangeListener;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ToggleButton;


public class PercentCurrentGraphFragment extends BaseGraphFragment {
	private static final int PAGE_COUNT = 2;
	private static final int PAGE_MAX_INDEX = PAGE_COUNT - 1;

	private ToggleButton priorButton;
	private ToggleButton percent91DaysDelinqentButton;
	private Button parameterButton;
	protected PercentCurrentAdapter adapter;

	private PercentCurrentParameterWindowListener parameterWindowListener;
	private BusinsessUnitWindowListener businsessUnitWindowListener;
	private Drawable buttonBackGround;
	private PageIndicator mPageIndicator;
	private PagedView pagedView;
	private boolean isPageEnable = true;
	private ViewGroup graphView;

	public PercentCurrentGraphFragment() {
		super();

	}

	public PercentCurrentGraphFragment(Customer customer, Handler mHandler) {
		super(customer, mHandler);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		graphView = (ViewGroup) inflater.inflate(R.layout.percent_current, container, false);		
		Log.d(TAG, "onCreateView...");
		return graphView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		adapter = new PercentCurrentAdapter(getActivity(), customer, graphView);		
		
		if (isPageEnable) {
			pagedView = (PagedView) getActivity().findViewById(R.id.paged_view);
			pagedView.setVisibility(View.VISIBLE);
			pagedView.setOnPageChangeListener(mOnPagedViewChangedListener);

			pagedView.setAdapter(adapter);

			mPageIndicator = (PageIndicator) getActivity().findViewById(R.id.page_indicator_next);
			mPageIndicator.setDotCount(PAGE_COUNT);
			mPageIndicator.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (pagedView.getCurrentPage() < PAGE_MAX_INDEX) {
						pagedView.scrollToNext();
					} else {
						pagedView.scrollToPrevious();
					}
				}
			});
			mPageIndicator.setActiveDot(pagedView.getCurrentPage());
		}else{
			graphView= (ViewGroup)getActivity().findViewById(R.id.graph_view);
			adapter.initGraphView(graphView);
			adapter.processListener(customer, graphView);
		}
		
		processGraphViewButtonEvent();
		
		Log.d(TAG, "onActivityCreated...");
	}

	public void refreshGraphView(Customer customer) {
		this.customer = customer;
		this.adapter.refreshData(customer);

	}

	public void processGraphViewButtonEvent() {

		priorButton = (ToggleButton) getActivity().findViewById(R.id.add_prior_button);

		priorButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				adapter.priorYear();

			}

		});

		percent91DaysDelinqentButton = (ToggleButton) getActivity().findViewById(R.id.add_91_days_delinqent_button);
		buttonBackGround = percent91DaysDelinqentButton.getBackground();

		percent91DaysDelinqentButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				adapter.percent91DaysDelinquent();

			}

		});
		parameterWindowListener = new PercentCurrentParameterWindowListener(getActivity(), customer, mHandler);

		businsessUnitWindowListener = new BusinsessUnitWindowListener(getActivity(), customer, mHandler);

		parameterButton = (Button) getActivity().findViewById(R.id.add_parameter_button);

		parameterButton.setOnClickListener(parameterWindowListener);

		// registerForContextMenu(parameterButton);

	}

	private OnPagedViewChangeListener mOnPagedViewChangedListener = new OnPagedViewChangeListener() {

		@Override
		public void onStopTracking(PagedView pagedView) {
		}

		@Override
		public void onStartTracking(PagedView pagedView) {
		}

		@Override
		public void onPageChanged(PagedView pagedView, int previousPage, int newPage) {
			mPageIndicator.setActiveDot(newPage);
		}
	};

}
